#include "Planet.h"
#include <math.h>

#define PI 3.14

Planet::Planet(std::string planetName)
{
	esize = 1.0;
	edist = 25.0;
	//Given a string it will set the parameters
	if (planetName == "Sun")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 1.0;
			colorB = 0.0;
			radius = 0.0;
			size = 10.0;
			orb_tilt = 0.0;
		}

		if (planetName == "Mercury")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 0.5;
			colorB = 0.0;
			radius = edist*0.3871;
			size = esize*0.38;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 0.24);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 7.0;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 2.0;
			rotateX = -2.0;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor =6.2 / 30;
		}

		if (planetName == "Venus")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 0.0;
			colorB = 1.0;
			radius = edist*0.7233;
			size = esize*0.95;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 0.62);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 3.4;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 177.3;
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = -1.5 / 30;
		}

		if (planetName == "Earth")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 0.0;
			colorG = 0.0;
			colorB = 1.0;
			radius = edist*1.0;
			size = esize*1.0;
			cur_deg = 0.0;
			deg_inc = 360.0 / 365.25; // standard earth day as basis
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 0.0;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 23.45;
			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = 365 / 30;

		}

		if (planetName == "Mars")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 0.0;
			colorB = 0.0;
			radius = edist*1.524;
			size = esize*0.53;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 1.88); // standard day for mars
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 1.85;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 25.19;

			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = 365 / 30;
		}

		if (planetName == "Jupiter")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 0.5;
			colorB = 0.0;
			radius = edist*5.203;
			size = esize*11.2;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 11.86);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 1.3;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 3.12;
			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = 730 / 30;
		}

		if (planetName == "Saturn")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			
			colorR = 0.2;
			colorG = 0.7;
			colorB = 0.5;
			
			radius = edist*9.53;
			size = esize*9.45;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 29.46);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 2.49;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 26.73;

			ps.changeSize(5500);

			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = 730 / 30;

		}
		
		if (planetName == "Uranus")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			
			colorR = 0.5;
			colorG = 0.2;
			colorB = 1.0;

			radius = edist*19.19;
			size = esize*4.01;
			cur_deg = 0.0;
			deg_inc = (360.0 / (365.25 * 84.01) );
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 0.77;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 97.86;

			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = -365 / 30;
		}

		if (planetName == "Neptune")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			
			colorR = 1.0;
			colorG = 0.2;
			colorB = 0.5;
			
			radius = edist*30.06;
			size = esize*3.96;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 164.79);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 1.77;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 29.6;

			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = 365 / 30;
		}
		if (planetName == "Pluto")
		{
			transX = 0.0;
			transY = 0.0;
			transZ = 0.0;
			colorR = 1.0;
			colorG = 1.0;
			colorB = 1.0;
			radius = edist*39.48;
			size = esize*.18;
			cur_deg = 0.0;
			deg_inc = 360.0 / (365.25 * 248.54);
			orb_ecc = 0.206;
			xradius = 2.0 * radius;
			yradius = 17.15;
		    zradius = sqrt(1-(orb_ecc*orb_ecc)) * xradius;
			orb_tilt = 122.46;

			
			rotateX = -orb_tilt;
			rotateY = 0.0;
			rotateZ = 0.0;
			rotationFactor = -60.8 / 30;
		}

		
}

void Planet::initializeOrbit()
{
	for(int i = 0; i<360; i++)
	{
		orbit[i][0] = xradius*sin(i*PI/180.0) - (orb_ecc*radius); 
		orbit[i][1] = radius*(yradius*PI/180.0)*sin(i*PI/180.0); // incline of elliptical
		orbit[i][2] = zradius*cos(i*PI/180.0);
	}
}

void Planet::initializeRings()
{
	for(int i = 0; i<ps.numParticles; i++)
	{
	double radius = rand() % 7 + 13;
	double theta  = rand() % 360;
	
	double tempX = radius*sin(theta*PI/180) - orb_ecc; // orb_ecc gives offset from center for ellipse
	double tempY = radius*(orb_tilt*PI/180)*sin(theta*PI/180);
	double tempZ = radius*cos(theta*PI/180);
	
	ps.updateSingleParticle(i,tempX, tempY, tempZ);
	}
}

//value is the solar time scale factor

void Planet::updatePlanet(double value)
{

	transX = xradius*sin(cur_deg*PI/180.0) - (orb_ecc*radius);
	transY = radius*(yradius*PI/180.0)*sin(cur_deg*PI/180.0); // incline of elliptical
	transZ = zradius*cos(cur_deg*PI/180.0);

	rotateY += rotationFactor;

	cur_deg = cur_deg + (deg_inc*value); // add dec_inc * value (value is time scale factor from main)
	
	if(cur_deg >= 360.0)
	{
		cur_deg = cur_deg - 360.0;
	}

}


void Planet::destructionMode(bool &DMode)
{
	if(size > 0.01){

	size = size*0.4;
	}

	if(size <= 0.01)
	{
	DMode = false;
	}

	blowUpPlanet();
}

void Planet::blowUpPlanet()
{
	ps.setupExplode(3000);
	
	for(int i = 0; i< 3000; i++)
	{
		ps.updateSingleParticle(i, transX, transY, transZ );
	}
}